package mt.demo.service.user;

import mt.demo.dao.SysFunctionDao;
import mt.demo.dao.UserDao;
import mt.demo.dao.UserRoleDao;
import mt.demo.domain.system.SysFunctionDomain;
import mt.demo.domain.user.UserDomain;
import mt.demo.domain.user.UserRoleDomain;
import mt.demo.service.comm.YomsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by watson on 2015/12/14.
 */
@Service
public class RoleService {
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;
    @Autowired
    private SysFunctionDao sysFunctionDao;

    //获取所有角色列表
    public List<UserRoleDomain> getAllRoleDomain(UserRoleDomain userRoleDomain)throws YomsException{
        List<UserRoleDomain> domains=userRoleDao.getDomainByCondition(userRoleDomain);
        if (domains.size()==0){
            throw new YomsException("角色列表为空");
        }
        else {
            for (UserRoleDomain domain:domains){
                if (domain.getUserRoleValue()==1){
                    domains.remove(domain);
                    break;
                }
            }
            return domains;
        }
    }


    //分配用户角色
    public void allotUserRole(List<Integer> userIds, long roleValue) throws YomsException {
        if (userIds.size() == 0) {
            throw new YomsException("用户集不能为空");
        } else {
            for (int id : userIds) {
                UserDomain domain = userDao.getDomainById(id);
                if ((domain.getUserRole() & roleValue) == 0) {
                    long value = domain.getUserRole() + roleValue;
                    domain.setUserRole(value);
                }
                userDao.updateDomain(domain);
            }
        }
    }

    //取消用户角色
    public void cancelUserRole(List<Integer> userIds, long roleValue) throws YomsException {
        if (userIds.size() > 0) {
            for (int id : userIds) {
                UserDomain domain = userDao.getDomainById(id);
                if ((domain.getUserRole() & roleValue) == roleValue) {
                    long value = domain.getUserRole() - roleValue;
                    domain.setUserRole(value > 0 ? value : 1);
                }
                userDao.updateDomain(domain);
            }
        } else {
            throw new YomsException("用户集不能为空");
        }
    }



    //为角色分配功能
    public void allotFunToRole(int roleId, int funId) throws YomsException {
        SysFunctionDomain sysFunctionDomain = sysFunctionDao.getDomainById(funId);
        UserRoleDomain roleDomain = userRoleDao.getDomainById(roleId);
        long sysFunValue = sysFunctionDomain.getSysFuncValue();
        long roleFunValue = roleDomain.getUserRoleFunction0();
        if ((sysFunValue & roleFunValue) == 0) {
            long value = sysFunValue + roleFunValue;
            roleDomain.setUserRoleFunction0(value);
        }
        userRoleDao.updateDomain(roleDomain);
    }

    //为角色撤销功能
    public void cancelFunToRole(int roleId, int funId) throws YomsException {
        SysFunctionDomain sysFunctionDomain = sysFunctionDao.getDomainById(funId);
        UserRoleDomain roleDomain = userRoleDao.getDomainById(roleId);
        long sysFunValue = sysFunctionDomain.getSysFuncValue();
        long roleFunValue = roleDomain.getUserRoleFunction0();
        if ((sysFunValue & roleFunValue) == roleFunValue) {
            long value = sysFunValue - roleFunValue;
            roleDomain.setUserRoleFunction0(value);
        }
        userRoleDao.updateDomain(roleDomain);
    }

    //根据用户ID获取角色列表
    public List<UserRoleDomain> getAllRoleByuserId(int userId)throws YomsException{
        List<UserRoleDomain> domains=userRoleDao.getRoleByUserId(userId);
        return domains;
    }
}
